package com.wyzz.hr.action;

import java.io.File;
import java.text.MessageFormat;
import java.util.*;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.bo.IInmatchBO;
import com.wyzz.hr.bo.IInmatchModelBO;
import com.wyzz.hr.io.extend.CommonParameters;
import com.wyzz.hr.io.extend.ICheckAndInsert;
import com.wyzz.hr.io.extend.IOMessageSingle;
import com.wyzz.hr.io.extend.IOMessages;
import com.wyzz.hr.io.extend.ReadExcelByImList;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Inmatch;
import com.wyzz.hr.pojo.InmatchModel;
import com.wyzz.hr.pojo.Iodef;
import com.wyzz.hr.pojo.Userinfo;
import com.wyzz.hr.util.Message;
import com.wyzz.hr.util.reflect.SetObjectProperty;

public class Import extends BaseAction {

	public Import() {
		msgTopList = new ArrayList();
		msgDetailList = new ArrayList();
		msgModelIlleagle = "\u6A21\u677F\u53C2\u6570\u975E\u6CD5";
		msgFileTypeError = "\u6587\u4EF6\u7C7B\u578B\u51FA\u9519";
		msgAllNum = "\u672C\u6B21\u5BFC\u5165\u603B\u8BA1{0}\u6761\u6570\u636E\uFF0C\u6210\u529F\u5BFC\u5165{1}\u6761\u6570\u636E";
		msgRepeatNum = "\u51FA\u73B0\u91CD\u590D\u8BB0\u5F55{0}\u6761";
		msgNoAuth = "\u60A8\u6CA1\u6709\u76F8\u5E94\u7684\u6743\u9650";
	}

	public String execute() throws Exception {
		IOMessages ioMessages = new IOMessages();
		IInmatchModelBO inmatchModelBO = (IInmatchModelBO) getBean("inmatchModelBO");
		InmatchModel inmatchModel = inmatchModelBO.loadObject(this.immId,
				new String[] { InmatchModel.PROP_IMM_IO });
		if (inmatchModel == null) {
			this.msgTopList.add(new Message(Integer.valueOf(-1),
					this.msgModelIlleagle));
			return "success";
		}
		Iodef iodef = inmatchModel.getImmIo();

		String authorityCondition = checkActionAuth("import", iodef.getIoName());
		if ("error".compareTo(authorityCondition) == 0) {
			this.msgTopList
					.add(new Message(Integer.valueOf(-1), this.msgNoAuth));
			return "success";
		}

		int hasTitle = 1 - inmatchModel.getImmNoTitle().intValue();
		IInmatchBO inmatchBO = (IInmatchBO) getBean("inmatchBO");
		List<Inmatch> imList = inmatchBO.getInputList(inmatchModel);
		inmatchModel.setImList(imList);
		List objectList;
		try {
			if (inmatchModel.getImmInputType().equals("excel")) {
				if (this.uploadFileName.substring(
						this.uploadFileName.lastIndexOf(".") + 1).compareTo(
						"xls") != 0) {
					this.msgTopList.add(new Message(Integer.valueOf(-1),
							this.msgFileTypeError));
					return "success";
				}
				ioMessages.setMsgFormat("excel");
				ReadExcelByImList readExcelByImList = new ReadExcelByImList(
						this.upload, imList, hasTitle, ioMessages);
				objectList = readExcelByImList.getContent();
			} else {
				this.msgTopList.add(new Message(Integer.valueOf(-1),
						this.msgFileTypeError));
				return "success";
			}
		} catch (Exception e) {
			e.printStackTrace();
			this.msgTopList.add(new Message(Integer.valueOf(-1),
					this.msgFileTypeError));
			return "success";
		}

		if (!ioMessages.hasErrorMsg()) {
			int totalNum = ((List) objectList.get(0)).size();

			Class classDefinition = Class.forName(iodef.getIoClassName());

			List resultList = new ArrayList();
			for (int i = 0; i < totalNum; ++i) {
				resultList.add(classDefinition.newInstance());
			}

			SetObjectProperty setObjectProperty = new SetObjectProperty();
			for (int i = 0; i < imList.size(); ++i) {
				Inmatch imTmp = (Inmatch) imList.get(i);
				List colTmp = (List) objectList.get(i);
				setObjectProperty.initial(classDefinition, imTmp.getImImb()
						.getImbFieldName());
				for (int j = 0; j < totalNum; ++j) {
					setObjectProperty.setProperty(resultList.get(j), colTmp
							.get(j));
				}

			}

			ICheckAndInsert checkAndInsert = (ICheckAndInsert) Class.forName(
					"com.wyzz.hr.io.extend." + iodef.getIoName()).newInstance();

			Map matchMap = new HashMap();
			for (Inmatch imTmp : imList) {
				matchMap.put(imTmp.getImImb().getImbFieldName(), imTmp);
			}

			Employee currEmp = ((Userinfo) getSession()
					.getAttribute("userinfo")).getEmployee();

			CommonParameters commonParas = new CommonParameters(inmatchModel,
					matchMap, ioMessages, currEmp, authorityCondition,
					new String[] { this.importParameter });

			int[] insertResult = checkAndInsert.insertTransmit(resultList,
					commonParas);
			if (insertResult[1] > 0) {
				ioMessages.addMessageToFirst(new IOMessageSingle(MessageFormat
						.format(this.msgRepeatNum, new Object[] { Integer
								.valueOf(insertResult[1]) }), Integer
						.valueOf(1), new Integer[0]));
			}
			ioMessages.addMessageToFirst(new IOMessageSingle(MessageFormat
					.format(this.msgAllNum, new Object[] {
							Integer.valueOf(totalNum),
							Integer.valueOf(insertResult[0]) }), Integer
					.valueOf(0), new Integer[0]));
		}

		ioMessages.sortMessages(new int[] { 0, 1 });
		List msgList = ioMessages.getShowMessageByTopDetail();
		this.msgTopList = ((List) msgList.get(0));
		this.msgDetailList = ((List) msgList.get(1));
		return "success";
	}

	public String getImmId() {
		return immId;
	}

	public void setImmId(String immId) {
		this.immId = immId;
	}

	public List getMsgTopList() {
		return msgTopList;
	}

	public List getMsgDetailList() {
		return msgDetailList;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getImportParameter() {
		return importParameter;
	}

	public void setImportParameter(String importParameter) {
		this.importParameter = importParameter;
	}

	private static final long serialVersionUID = 1L;
	private String importParameter;
	private File upload;
	private String uploadFileName;
	private String uploadContentType;
	private String immId;
	private List msgTopList;
	private List msgDetailList;
	String msgModelIlleagle;
	String msgFileTypeError;
	String msgAllNum;
	String msgRepeatNum;
	String msgNoAuth;
}
